package com.yu.rabbitmq.two;

import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.yu.rabbitmq.utils.RabbitMqUnits;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Worker03 {
    public static final String TASK_QUEUE_NAME = "ack_queue";

    public static void main(String[] args) {
        try {
            Channel channel = RabbitMqUnits.getChannel();
            channel.basicQos(1);
            System.out.println("C2等待接受消息，时间较长");
            DeliverCallback callback = (consumerTag, delivery) -> {
                try {
                    //模拟睡10秒
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                String message = new String(delivery.getBody());
                System.out.println("接收到的消息"+message);
                /*
                basicAck 表示手动应答
                param1：消息的标记 tag
                param2：是否批量
                 */
                channel.basicAck(delivery.getEnvelope().getDeliveryTag(),false);
            };
            CancelCallback cancelCallback = (consumerTag)->{
                System.out.println("消息消息被中断");
            };
            //autoAck=false 表示手动应答
            channel.basicConsume(TASK_QUEUE_NAME,false,callback,cancelCallback);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    }
}
